#!/usr/bin/env bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.

# Script to setup environment variables and deployer config to run CI tasks.
#
# Usage: setenvconfig ENV

set -eu
set -o pipefail

ROOT=$(dirname $0)/..

write_env()             { cat > $ROOT/.env; }
write_deployer_config() { cat > $ROOT/deployer-config.yml; }

# detect dev local execution
if [[ ${BUILD_ID:-} == "" ]]; then
  BUILD_NUMBER=local-dev-$(date +%s)t
  BUILD_TAG=local-dev-$(date +%s)t
  # need to be declared but not required for local execution
  VAULT_ROLE_ID="dev"
  VAULT_SECRET_ID="dev"
fi

# main

environment=${1:-""}
case $environment in

######################################
  e2e/aks)
######################################

write_env <<ENV
REGISTRY = cloudonk8s.azurecr.io
REPOSITORY = operators
IMG_SUFFIX = -ci

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE = /go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE = $JKS_PARAM_OPERATOR_IMAGE
TEST_TIMEOUT = 10m

PIPELINE=e2e/aks
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=aks
CLUSTER_NAME=$BUILD_TAG
KUBERNETES_VERSION=default
ENV
write_deployer_config <<CFG
id: aks-ci
overrides:
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;

######################################
  e2e/custom-operator-image)
######################################

write_env <<ENV
REGISTRY = eu.gcr.io
REPOSITORY = $GCLOUD_PROJECT
IMG_SUFFIX = -ci

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE = /go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE = $JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/custom-operator-image
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=eck-e2e-$BUILD_NUMBER
KUBERNETES_VERSION=$JKS_PARAM_K8S_VERSION
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  clusterName: eck-e2e-$BUILD_NUMBER
  kubernetesVersions: $JKS_PARAM_K8S_VERSION
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/gke-k8s-versions)
######################################

clusterVersion=$2
clusterName=$3

write_env <<ENV
REGISTRY = eu.gcr.io
REPOSITORY = $GCLOUD_PROJECT
IMG_SUFFIX = -ci

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE = /go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE = $JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/gke-k8s-versions
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=${clusterName}
KUBERNETES_VERSION="${clusterVersion}"
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  operation: create
  kubernetesVersion: "${clusterVersion}"
  clusterName: ${clusterName}
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/kind-k8s-versions)
######################################

kindNodeImage=$2

write_env <<ENV
REGISTRY = eu.gcr.io
REPOSITORY = $GCLOUD_PROJECT
IMG_SUFFIX = -ci

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE = /go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE = $JKS_PARAM_OPERATOR_IMAGE
KIND_NODE_IMAGE = ${kindNodeImage}

PIPELINE=e2e/kind-k8s-versions
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=kind
CLUSTER_NAME=kind-$BUILD_NUMBER
KUBERNETES_VERSION=default
ENV
;;

######################################
  e2e/master)
######################################

write_env <<ENV
REGISTRY = eu.gcr.io
REPOSITORY = $GCLOUD_PROJECT
IMG_SUFFIX = -ci

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE = /go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

PIPELINE=e2e/master
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=eck-e2e-$BUILD_NUMBER
KUBERNETES_VERSION=default
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  clusterName: eck-e2e-$BUILD_NUMBER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/ocp)
######################################

write_env <<ENV
REGISTRY = eu.gcr.io
REPOSITORY = $GCLOUD_PROJECT
IMG_SUFFIX = -ci

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE = /go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE = $JKS_PARAM_OPERATOR_IMAGE
TEST_TIMEOUT = 10m

PIPELINE=e2e/ocp
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=ocp
CLUSTER_NAME=$BUILD_TAG
KUBERNETES_VERSION=default
ENV
write_deployer_config <<CFG
id: ocp-ci
overrides:
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  ocp:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/stack-versions)
######################################

clusterName=$2
stackVersion=$3
isSnapshotVersion=$(case $stackVersion in (*-SNAPSHOT)  echo Yes;; esac)
operatorImage=${OPERATOR_IMAGE:-$JKS_PARAM_OPERATOR_IMAGE}

write_env <<ENV
REGISTRY = eu.gcr.io
REPOSITORY = $GCLOUD_PROJECT
IMG_SUFFIX = -ci
IS_SNAPSHOT_BUILD = ${isSnapshotVersion}
TEST_LICENSE = /go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE = ${operatorImage}
STACK_VERSION = ${stackVersion}

PIPELINE=e2e/stack-versions
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=${clusterName}
KUBERNETES_VERSION=default
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  operation: create
  clusterName: ${clusterName}
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  pr)
######################################

write_env <<ENV
REGISTRY = eu.gcr.io
REPOSITORY = $GCLOUD_PROJECT
IMG_SUFFIX = -ci

TESTS_MATCH = TestSmoke
MONITORING_SECRETS = /go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

PIPELINE=pr
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=eck-pr-$BUILD_NUMBER
KUBERNETES_VERSION=default
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  clusterName: eck-pr-$BUILD_NUMBER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  build)
######################################

  WORKSPACE=${WORKSPACE:-$ROOT}

  # valid the existence of the $WORKSPACE/VERSION file
  if [[ ! -f $WORKSPACE/VERSION ]]; then
    echo "$WORKSPACE/VERSION not found"
    exit
  fi

  # choose between a nightly or a release build based on TAG_NAME
  if [[ ${TAG_NAME:-} == "" ]]; then

###############
# nightly build
###############

# IMG_VERSION is generated

write_env <<ENV
REGISTRY = docker.elastic.co
REPOSITORY = eck-snapshots
IMG_NAME = eck-operator

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key

ELASTIC_DOCKER_LOGIN = eckadmin

SNAPSHOT = true
IMG_VERSION = $(cat $WORKSPACE/VERSION)-$(date +%F)-$(git rev-parse --short --verify HEAD)
ENV

  else

###############
# release build
###############

write_env <<ENV
REGISTRY = docker.elastic.co
REPOSITORY = eck
IMG_NAME = eck-operator

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key

ELASTIC_DOCKER_LOGIN = eckadmin

SNAPSHOT = false
IMG_VERSION = $TAG_NAME
ENV

  fi
;;

######################################
  dev/build)
######################################

write_env <<ENV
REGISTRY = docker.elastic.co
REPOSITORY = eck-snapshots
IMG_SUFFIX = -dev-ci
IS_SNAPSHOT_BUILD = yes
ELASTIC_DOCKER_LOGIN = eckadmin

GO_TAGS = release
export LICENSE_PUBKEY = /go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
ENV
;;


######################################
  cleanup/gke)
######################################

write_deployer_config <<CFG
id: gke-ci
overrides:
  operation: delete
  clusterName: $JKS_PARAM_GKE_CLUSTER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  cleanup/aks)
######################################

write_deployer_config <<CFG
id: aks-ci
overrides:
  operation: delete
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;

######################################
  cleanup/ocp)
######################################

write_deployer_config <<CFG
id: ocp-ci
overrides:
  operation: delete
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  ocp:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  *)
    echo -e "\nError: invalid environment\n"
    echo 'Environments available:'
    grep -ro '^\s\s*[a-z].*)$' $0 | sed "s/)//"
    exit 1
  ;;

esac
